class Bangumi < ActiveRecord::Base
  belongs_to :bangumi_type
  has_and_belongs_to_many :works, :join_table => "bangumi_works"
  has_many :bangumi_works
  validates_presence_of :name, :message => 'name is null'

  def check_hot_status
     if status.zero?
       works = Work.find(bangumi_works.collect(&:work_id))
       weeklies = Weekly.all :conditions => ["wid > ?", works.collect(&:wid).min]
       wids = []
       weeklies.each do |w|
         wids += WeeklyResult.all(:select => "weekly_result_works.wid", :include => [:weekly_result_work],
                       :conditions => ["weekly_results.weekly_id = ? and weekly_result_works.wid in (?) and weekly_results.score >= ?", w.id, works.collect(&:wid), w.score_from]).collect(&:wid)
         wids.uniq!
         if wids.size > 1
           update_attribute :status, 1
           break
         end
       end
     end
  end
end
